/**
 * 学生自报岗位
 * 
 * 学生自报岗位后，就自动插入一条岗位记录，需要人数和报名人数都是1。同时设置公司网址字段为XXXX，用来识别该岗位是自报岗位还是学院岗位
 * 
 * 这里需求文档设置的不合理，岗位类型是用 type 来标记的  类型 1 校外岗位， 0 校内岗位, 2学生自报岗位
 * 
 * 
 * /student/addjob
 */

  
const Companies = require('../../../models/companys')
const Students = require('../../../models/internshipStudents')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager


const add_Job = async function(req, res) {    
    try {
        if (!req.body.year_id) {
            $.sendMessage(res, 1015, '参数 year_id 不存在')
            return
        }

        if (!req.body.company_name) {
            $.sendMessage(res, 1015, '参数 company_name 不存在')
            return
        }

        if (!req.body.job_name) {
            $.sendMessage(res, 1015, '参数 job_name 不存在')
            return
        }

        // 查看学生是否被锁定
        const is_locked = await Students.findOne({
            where: {
                student_user_name: req.user_name
            }
        })
        if (is_locked == null) {
            $.sendMessage(res, 1015, '学生不存在id')
            return
        } else if (is_locked.dataValues.is_locked == 0) {
            $.sendMessage(res, 1015, '您被锁定，不能自报岗位')
            return
        }

        var student_info = await Students.findOne({
            where: {
                student_year: req.body.year_id,
                student_user_name: req.user_name
            }
        })

        if (student_info == null) {
            $.sendMessage(res, 1015, '你不在该年度里，请联系老师')
            return
        }

        // 检查是否已经选题
        if (student_info.dataValues.student_status != '0') {
            $.sendMessage(res, 1015, '你已经报名实习岗位了')
            return
        }

        // 创建岗位，学生报名该岗位
        // 事务
        var company_obj =  {}
        company_obj['company_name'] = req.body.company_name
        company_obj['job_name'] = req.body.job_name
        company_obj['company_year'] = req.body.year_id
        company_obj['company_website'] = 'XXXXXX'
        company_obj['remark'] = '学生自报岗位'
        company_obj['current_student'] = 1
        company_obj['student_limit'] = 1
        company_obj['create_student'] = req.user_name
        company_obj['type'] = 2

        return sequelize.transaction(function (t) {

            return Companies.create(
            company_obj
            , 
            {transaction: t}).then(function (user) {
                $.log('user', user)
            return Students.update(
                {
                    company_id: user.id,
                    company_name: req.body.company_name,
                    student_status: '1'

                },
                {
                    where: {
                        student_year: req.body.year_id,
                        student_user_name: req.user_name
                    }
                }
                , {transaction: t})
            })
        })
        // 请求完整完成
        .then(function (result) {
            res.json({
                code: 200,
                message: '自报岗位报名成功',
                result: result
            })
            utils.create_a_log(req, '学生实习', '自报岗位报名')
            return
        })
        // 处理sequalize异常
        .catch(function (err) {
            res.json({
                code: 1017,
                message: '自报岗位报名失败，请联系管理员',
                err: 'err' + err
            })
            return
        })
       
    }

    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '报名题目', '失败')
        return
    }
}

module.exports = add_Job